package com.mms.service.impl;

import cn.hutool.crypto.digest.DigestUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mms.mapper.SysUserMapper;
import com.mms.model.pojo.SysUser;
import com.mms.service.SysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @auth wzy
 * @date 2023/11/21 11:42
 */
@Service
public class SysUserServiceImpl implements SysUserService {

    @Autowired
    private SysUserMapper sysUserMapper;

    @Override
    public SysUser login(String loginName, String password) {
        SysUser sysUser = sysUserMapper.selectByLoginName(loginName);

        if (sysUser == null) {
            return null;
        }

        String digestHex = DigestUtil.md5Hex(password);

        if (!digestHex.equals(sysUser.getPassword())){
            return null;
        }

        return sysUser;
    }

    @Override
    public PageInfo<SysUser> pageList(SysUser sysUserParam,Integer pageNum,Integer pageSize) {

        PageHelper.startPage(pageNum, pageSize);
        List<SysUser> userList = sysUserMapper.pageList(sysUserParam);
        PageInfo<SysUser> pageInfo = new PageInfo<SysUser>(userList);
        return pageInfo;
    }

    @Override
    public int count(SysUser sysUserParam) {
        return sysUserMapper.count(sysUserParam);
    }


    @Override
    public int save(SysUser sysUser) {

        if (sysUser.getUserId() == null) {
            // 做新增
            sysUser.setPassword(DigestUtil.md5Hex(sysUser.getPassword()));
            return sysUserMapper.add(sysUser);
        } else {
            // 修改
//            return sysUserMapper.update(sysUser);
        }
        return 0;
    }
}
